package com.tanklab.signature.controller;

import com.tanklab.signature.common.util.JwtUtils;
import com.tanklab.signature.common.util.ResultCode;
import com.tanklab.signature.ds.resp.CommonResp;
import com.tanklab.signature.service.SignatureStorageService;
import com.tanklab.signature.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/signature/user")
@Api(tags = "用户签名接口")
public class UserSignatureController {

    @Autowired
    private SignatureStorageService signatureStorageService;

    @Autowired
    private UserService userService;

    @Autowired
    private JwtUtils jwtUtils;

    @ApiOperation("上传用户签名")
    @PostMapping("/signature/upload")
    public CommonResp<Map<String, String>> uploadSignature(
            @RequestParam("file") MultipartFile file,
            HttpServletRequest request) {

        CommonResp<Map<String, String>> resp = new CommonResp<>();

        // 从请求头获取token
        String token = request.getHeader("Authorization");
        if (token != null && token.startsWith("Bearer ")) {
            token = token.substring(7);
        } else {
            resp.setRet(ResultCode.TOKEN_MISS);
            resp.setMsg("未提供有效的token");
            return resp;
        }

        // 从Token提取openid
        String openid = jwtUtils.getOpenidFromToken(token);

        // 验证文件是否为空
        if (file.isEmpty()) {
            resp.setRet(ResultCode.PARAM_ERROR);
            resp.setMsg("签名文件不能为空");
            return resp;
        }

        // 验证文件类型
        String contentType = file.getContentType();
        if (contentType == null || !contentType.startsWith("image/")) {
            resp.setRet(ResultCode.PARAM_ERROR);
            resp.setMsg("只允许上传图片文件作为签名");
            return resp;
        }

        try {
            // 存储签名图片
            String signatureUrl = signatureStorageService.storeSignature(file);

            // 上传签名并上链
            return userService.uploadSignature(openid, signatureUrl);

        } catch (IOException e) {
            resp.setRet(ResultCode.ERROR);
            resp.setMsg("签名上传失败: " + e.getMessage());
            return resp;
        }
    }

    @ApiOperation("获取用户签名")
    @GetMapping("/signature/get")
    public CommonResp getUserSignature(HttpServletRequest request) {
        // 从请求头获取token
        String token = request.getHeader("Authorization");
        if (token == null || !token.startsWith("Bearer ")) {
            CommonResp resp = new CommonResp<>();
            resp.setRet(ResultCode.TOKEN_MISS);
            resp.setMsg("未提供有效的token");
            return resp;
        }

        // 从Token提取openid
        token = token.substring(7);
        String openid = jwtUtils.getOpenidFromToken(token);

        // 获取用户签名信息
        return userService.getUserSignature(openid);
    }
}